package com.aca.market.service.impl;

import com.aca.market.entity.Catalog;
import com.aca.market.entity.IndentItem;
import com.aca.market.entity.TypeItem;
import com.aca.market.mapper.CatalogMapper;
import com.aca.market.mapper.ItemMapper;
import com.aca.market.mapper.TypeItemMapper;
import com.aca.market.service.ICatalogService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

@Transactional
@Service
public class CatalogServiceImpl extends ServiceImpl<CatalogMapper,Catalog> implements ICatalogService {

    @Autowired
    CatalogMapper catalogMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    TypeItemMapper typeItemMapper;

    //itemId与分类名的映射表
    @Override
    public Map<Long,String> getItemIdTypeNameMap(){
        return typeItemMapper.selectList(new QueryWrapper<TypeItem>()
                .select("item_id", "type_name"))
                .parallelStream()
                .collect(Collectors.toMap(TypeItem::getItemId,TypeItem::getTypeName));
    }

    @Override
    public List<String> getAllNames(){
        return catalogMapper.selectList(new QueryWrapper<Catalog>()
                .select("distinct type_name"))
                .parallelStream()
                .map(catalog -> catalog.getTypeName())
                .collect(Collectors.toList());
    }

    @Override
    //获取分类id:分类名映射表
    public ConcurrentMap<Long,String> getAllIdNameMap(){
        List<Catalog> catalogs = catalogMapper
                        .selectList(new QueryWrapper<Catalog>()
                        .eq("status", 1));
        ConcurrentMap<Long,String> map = new ConcurrentHashMap<>();
        for (Catalog catalog : catalogs) {
            map.put(catalog.getTypeId(),catalog.getTypeName());
        }
        return map;
    }

    @Override
    //获取分类id:分类名映射表
    public ConcurrentMap<Long,String> typeIdNameMap(List<IndentItem> indentItems){
        ArrayList<Long> typeIdList = indentItems.parallelStream()
                .mapToLong((o) -> o.getItemType())
                .boxed()
                .collect(Collectors.toCollection(ArrayList::new));

        List<Catalog> catalogs = catalogMapper.selectList(new QueryWrapper<Catalog>()
                .in("type_id", typeIdList));
        ConcurrentMap<Long,String> typeName = new ConcurrentHashMap<>();
        for (Catalog catalog : catalogs) {
            typeName.put(catalog.getTypeId(),catalog.getTypeName());
        }
        return typeName;
    }

    @Override
    public IPage<Catalog> selectItemPage(Page<Catalog> page, Wrapper<Catalog> queryWrapper) {
        return catalogMapper.selectPage(page,queryWrapper);
    }

    public Map<Catalog,List<Catalog>> getCatalogMap(){
        //1找到所有父目录id(parent=0) 2 遍历父 建立 父：子 的映射
        Map<Catalog,List<Catalog>> catalogMap = new HashMap<>();
        List<Catalog> parents = catalogMapper.selectList(new QueryWrapper<Catalog>().eq("parent_id", 0));

        for (Catalog catalog : parents) {
            List<Catalog> sons = catalogMapper.selectList(new QueryWrapper<Catalog>().eq("parent_id", catalog.getTypeId()));
            catalogMap.put(catalog,sons);
        }
        return catalogMap;
    }
}
